<?php
use Illuminate\Support\Facades\Cookie;
use Illuminate\Http\Request;
use DB\DB;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/13
 * Time: 21:51
 */
if(! function_exists('test')){
    if(! function_exists('return_respone')){
        function ajax_respone($code,$msg,$data=[]){
            $result=[
                'success'=>$code,
                'msg'=>$msg,
                'data'=>$data
            ];
            return json_encode($result);
        }
    }
    if( ! function_exists('back')){
        function back($msg){
            header("content-type:text/html;charset=utf-8");
            echo "<script>alert('$msg');window.history.go(-1);</script>";
            exit;
        }
    }
    if( ! function_exists('redirect')){
        function redirect($url,$msg){
            header("content-type:text/html;charset=utf-8");
            echo "<script>alert('$msg');window.location.href='$url';</script>";
            exit;
        }
    }
    if(!function_exists('json')){
        function json($array){
            $code=$array['code'];
            $data=isset($array['data'])?$array['data']:[];
            $msg=$array['msg'];
            $result=[
                'code'=>$code,
                'data'=>$data,
                'msg'=>$msg
            ];
            return json_encode($result);
        }
    }
    if(!function_exists('input')){
        function input($key=null){
            return request()->input($key);
        }
    }
    if(!function_exists('cookie1')){
        function cookie1($name,$value=null,$time=30){
            if(empty($value) && empty($name)){
                die('无效');
            }else if(empty($value)){

                return get_cookie($name);
            }else{
                return set_cookie($name,$value,$time);
            }
            return null;
        }
    }
    if(!function_exists('set_cookie')){
        function set_cookie($key,$value,$time){
            Cookie::queue($key,$value,$time);
        }
    }
    if(!function_exists('get_cookie')){
        function get_cookie($key){
           return request()->cookie($key);
        }
    }
    if(!function_exists('db')){
        function db($table){
            if(empty(DB::$_connections)){
                $config=Config::get('kefu.db_conf');
                DB::init($config);
            }
            return DB::connection('default')->table($table);
        }
    }
}